package com.promis.widget.MyPopupWindow;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.promis.widget.R;

import java.util.List;

/**
 * Created by nc on 2018/7/27.
 */

public abstract class CommonPopupWindow {
    protected Context context;
    protected View contentView;
    protected PopupWindow mInstance;

    public PopupWindowAdapter popupWindowAdapter;
    protected RecyclerView recyclerView;
    protected List<String> stringList;

    public CommonPopupWindow(Context c, int w, int h) {
        context = c;
        contentView = LayoutInflater.from(c).inflate( R.layout.activity_popupwindow, null, false);
        recyclerView = contentView.findViewById(com.promis.widget.R.id.activity_popupwindow_rylv_container);
        popupWindowAdapter = new PopupWindowAdapter(c);
        initView();
        initEvent();
        mInstance = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, h, true);
//        mInstance.setAnimationStyle(R.style.animTranslate);
        initWindow();
    }

    public View getContentView() { return contentView; }
    public PopupWindow getPopupWindow() { return mInstance; }

    protected abstract void initView();
    protected abstract void initEvent();

    public List<String> getStringList() {
        return stringList;
    }

    public void setStringList(List<String> stringList) {
        this.stringList = stringList;
    }

    protected void initWindow() {
        mInstance.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mInstance.setOutsideTouchable(true);
        mInstance.setTouchable(true);
    }

    public void showBashOfAnchor(View anchor, LayoutGravity layoutGravity, int xmerge, int ymerge) {
        int[] offset=layoutGravity.getOffset(anchor, mInstance);
        mInstance.showAsDropDown(anchor, offset[0]+xmerge, offset[1]+ymerge);
    }

    public void showAsDropDown(View anchor, int xoff, int yoff) {
        mInstance.showAsDropDown(anchor, xoff, yoff);
    }

    public void showAtLocation(View parent, int gravity, int x, int y) {
        mInstance.showAtLocation(parent, gravity, x, y);
    }

    public void closePopupWindow(){
        if(getPopupWindow() != null) getPopupWindow().dismiss();
    }
}
